package com.wwtech.epp.ui.activity.login;

import android.content.Intent;
import android.os.Bundle;

import com.ninjia.ninjiabaselibrary.utils.SpUtil;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.ui.activity.login.component.DaggerSplashComponent;
import com.wwtech.epp.ui.activity.login.contract.SplashContract;
import com.wwtech.epp.ui.activity.login.module.SplashModule;
import com.wwtech.epp.ui.activity.login.presenter.SplashPresenter;
import com.wwtech.epp.ui.activity.main.MainActivity;

import javax.inject.Inject;

import butterknife.ButterKnife;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.login
 * @Description: $description
 * @date 2017/08/01 16:44:18
 */

public class SplashActivity extends BaseActivity implements SplashContract.View {

    @Inject
    SplashPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        setToorBar(false);
    }

    @Override
    protected void initData() {
        mPresenter.observeJump();
        mPresenter.getPermission();
        mPresenter.doAutoLogin();
        mPresenter.getLastVersion();
    }

    @Override
    protected void setupActivityComponent() {
        DaggerSplashComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .splashModule(new SplashModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(SplashContract.SplashContractPresenter presenter) {
        mPresenter = (SplashPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void loginSuccees() {
        startActivity(MainActivity.class);
        overridePendingTransition(R.anim.main_activity_in, R.anim.splash_activity_out);
        AppConfig.getInstance().mAppActivityManager.finishActivity();
    }

    @Override
    public void jumpToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra("flag", "splash");
        startActivity(intent);
        AppConfig.getInstance().mAppActivityManager.finishActivity();
    }

    @Override
    public void iumpToGuest() {
        startActivity(GuestActivity.class);
        AppConfig.getInstance().mAppActivityManager.finishActivity();
    }

    @Override
    public void setToken(String token) {
        SpUtil.putString(this, "loginToken", token);
        KLog.i("持久化token");
    }

}